<template>
  <div>
    <el-dialog
      :title="info.isAdd ? '分类添加' : '分类修改'"
      :visible.sync="info.isshow"
      @close="close"
    >
      {{ user }}
      <el-form :model="user">
        <el-form-item label="上级分类" label-width="120px">
          <el-select v-model="user.pid">
            <el-option label="顶级菜单" :value="0"></el-option>
            <el-option
              v-for="item in roleList"
              :key="item.id"
              :value="item.id"
              :label="item.catename"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品分类名称" label-width="120px">
          <el-input v-model="user.catename" autocomplete="off"></el-input>
        </el-form-item>

        <!-- <el-form :model="user"> -->
        <el-form-item label="图片" label-width="120px">
          <div class="pro">
            <h3>+</h3>
            <img :src="imgUrl" v-if="imgUrl">
            <input type="file"  @change="getFile"/>
          </div>
        </el-form-item>
        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="user.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <!-- </el-form> -->

      <div slot="footer" class="dialog-footer">
        <el-button>取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="handleNodeClick"
          >添 加</el-button
        >
        <el-button type="primary" v-else @click="update">修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  requserinfo,
  requseredit,
  reqcateadd,
reqcatelist,
reqcateinfo,
reqcateedit,
} from "../../../http/api";
import path from "path";
import { successAlert, errorAlert } from "../../../utils";
export default {
  props: ["info"],
  methods: {
    handleNodeClick() {
      // requseradd(this.user).then((res) => {
      //   // console.log(res);
      //   successAlert(res.data.msg);
      //   this.$emit("init");
      //   this.empty();
      //   this.info.isshow = false;
      // });
      reqcateadd(this.user).then(res => {
        // console.log(res);
        successAlert(res.data.msg);
        this.$emit("init");
        this.empty();
        this.info.isshow = false;
      })

    },
    getFile(e) {
      let file = e.target.files[0];
      //验证大小 file.size 单位是B    1M=1024KB 1KB=1024B 1B=8b
      if (file.size > 2 * 1024 * 1024) {
        errorAlert("文件最大2M");
        return;
      }

      let imgArr = [".png", ".jpeg", ".jpg", ".gif"];
      let extname = path.extname(file.name); // .png
      if (!imgArr.includes(extname)) {
        errorAlert("请上传正确的图片格式");
        return;
      }
      // 生成图片地址
      this.imgUrl = URL.createObjectURL(file)
      //存到user，后续发送给后端
      this.user.img=file;
        console.log(this.user.img);
    },
    getOne(id) {
      reqcateinfo({ id: id }).then((res) => {
        this.user = res.data.list;
        this.user.id = id;
        this.imgUrl = this.$pre + this.user.img;
      });
    },
    empty() {
      this.user = {
        pid: '',
        catename: "",
        img: "",
        status: 1,
      };
      this.imgUrl = '';
    },
    close() {
      if (!this.info.isAdd) {
        this.empty();
      }
    },
    update() {
      // this.user.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
      reqcateedit(this.user).then((res) => {
        // console.log(res);
        successAlert(res.data.msg);
        this.empty();
        this.info.isshow = false;
        this.$emit("init");
      });
    },
  },
  mounted() {
    // reqmenulist({ istree: true }).then((res) => {
    //   this.treedata = res.data.list ? res.data.list : [];
    // });
    // reqrolelist().then((res) => {
    //   if (res.data.code === 200) {
    //     // console.log(res);
    //     this.roleList = res.data.list ? res.data.list : [];
    //   }
    // });
    reqcatelist({istree:true}).then(res => {
      console.log('####',res);
      this.roleList = res.data.list;
    })
  },

  data() {
    return {
      user: {
        pid: '',
        catename: "",
        img: "",
        status: 1,
      },
      imgUrl:'',
      roleList: [],
    };
  },
};
</script>

<style scoped>
.pro {
  width: 200px;
  height: 200px;
  border: 1px solid #000;
  position: relative;
}
.pro input {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  cursor: pointer;
  opacity: 0;
}
.pro img{
  width: 100%;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
}
.pro h3 {
  text-align: center;
  line-height: 200px;
}
</style>